package com.yangrd.airship.generator.support.impl;

import com.yangrd.airship.generator.support.BasePageGeneratorConst;
import com.yangrd.airship.generator.support.PageGeneratorDefineMetaHook;
import com.yangrd.airship.generator.support.utils.StringUtils;
import com.yangrd.airship.generator.page.PageGeneratorDefine;
import lombok.RequiredArgsConstructor;

import java.time.LocalDate;

/**
 * @author yangrd
 * @date 2021/11/9
 */
@RequiredArgsConstructor
public class BasePgdMetaHook implements PageGeneratorDefineMetaHook {

    private final String basePackage;

    @Override
    public void handler(PageGeneratorDefine tableGeneratorDefine, Object target) {
        if(target instanceof FreemarkerPageTemplateResolver.TemplateInfo){
            FreemarkerPageTemplateResolver.TemplateInfo templateInfo = (FreemarkerPageTemplateResolver.TemplateInfo) target;
            tableGeneratorDefine
                    .putMeta(BasePageGeneratorConst.BASE_PACKAGE,basePackage+"."+tableGeneratorDefine.getName().toLowerCase())
                    .putMeta(BasePageGeneratorConst.CRATED_DATE, LocalDate.now())
                    .putMeta(BasePageGeneratorConst.ID_TYPE, "String")
                    .putMeta(BasePageGeneratorConst.ENTITY_NAME, tableGeneratorDefine.getName())
                    .putMeta(BasePageGeneratorConst.MODULE_NAME, templateInfo.getPath().replaceAll("/","."));
        }
    }
}
